#!/bin/sh -e
# git-cache-meta -- file owner and permissions caching, minimalist approach
# src: http://permalink.gmane.org/gmane.comp.version-control.git/105133%7C%7CPATCH

: ${GIT_CACHE_META_FILE=.git_cache_meta} #simpler not to git-add it
case $@ in
    --store|--stdout)
	case $1 in --store) exec > $GIT_CACHE_META_FILE; esac
  grpid="`id -g -- "${USER?}"`"
  #will only show the permissions that are different from current user/group
	git ls-files|xargs -I{} find {} \
	    \( -user ${USER?} -o -printf "chown %u '%p'\n" \) \
	    \( -group $grpid -o -printf "chgrp %g '%p'\n" \) \
	    \( \( -type l -o -perm 755 -o -perm 644 \) \
			-o -printf "chmod %#m '%p'\n" \);;#requires GNU Find
    --apply) sh -e $GIT_CACHE_META_FILE;;
    *) 1>&2 cat <<EOF; exit 1;;
Usage: $0 --store|--stdout|--apply #Example:
# git bundle create mybundle.bdl master; git-cache-meta --store
# scp mybundle.bdl .git_cache_meta machine2: #then on machine2:
# git init; git pull mybundle.bdl master; git-cache-meta --apply
EOF
esac

# NOTE:
# ${USER?} does this when USER is unset: bash: USER: parameter null or not set
# and exitcode is 1
# sh -e scriptfile  is execute scriptfile (eg. even if it's not +x) works only on scripts; eg. sh -e ls  won't work
